package net.risesoft.risebi.collection.web.admin;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;
import net.risesoft.risebi.collection.model.ReportPeriod;
import net.risesoft.risebi.collection.model.ReportPeriodType;
import net.risesoft.risebi.collection.service.ReportPeriodManager;
import net.risesoft.risebi.collection.service.ReportPeriodTypeManager;
import net.risesoft.risebi.collection.service.ReportWindowPeriodManager;
import net.risesoft.risebi.collection.web.BaseAction;
import net.risesoft.risebi.commons.util.GUID;
import net.risesoft.risebi.commons.util.JSONUtil;
import net.sf.json.JSONObject;
import org.apache.commons.beanutils.LazyDynaBean;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

/**
  * 本类提供报告期管理Action
 * @author 丁兆俊</a>
 * @version $Revision$
 * 
 */
@Namespace("/admin/ReportPeriodInstanceAction")
public class ReportPeriodInstanceAction extends BaseAction{
	private static final long serialVersionUID = 2545221332927052381L;
	
	private ReportPeriod reportPeriod;	
	private ReportPeriodManager reportPeriodManager;	
	private ReportPeriodTypeManager reportPeriodTypeManager;
	private ReportWindowPeriodManager reportWindowPeriodManager;
	
	private String ModalDialogFieldValue;
	private String startdate;
	private int interval;
	
	@Action(value="prepareSelectParentPeriod",
			results={@Result(name="success", location="/admin/reportperiod/selectParentPeriod.jsp")}
	)
	public String prepareSelectParentPeriod(){
		return SUCCESS;
	}

	@Action(value="prepareSelectPreviousPeriod",
			results={@Result(name="success", location="/admin/reportperiod/selectPreviousPeriod.jsp")}
	)
	public String prepareSelectPreviousPeriod(){
		return SUCCESS;
	}

	@Action(value="prepareSelectLastyearPeriod",
			results={@Result(name="success", location="/admin/reportperiod/selectLastyearPeriod.jsp")}
	)
	public String prepareSelectLastyearPeriod(){
		return SUCCESS;
	}

	@Action(value="refreshJson")
	public String refreshJson() throws Exception {
		long startTime = System.currentTimeMillis();
		
		try {
			if ("jsonReportPeriods4List".equalsIgnoreCase(this.whichJson)) {
				Map map = reportPeriodManager.getPagedReportPeriod(this.node, this.start, this.limit);				
				this.json = JSONUtil.getJsonString("include","count,list,guid,code,name,timetype,begin_time,end_time,duration,order_in_year,parent_termGuid,previous_termGuid,lastyear_termGuid",map);
			}else if("jsonReportPeriods4Combo".equalsIgnoreCase(this.whichJson)) {
				Map map = reportPeriodManager.getPagedReportPeriod4Combo(this.node, this.start, this.limit);				
				this.json = JSONUtil.getJsonString("include","count,list,guid,name",map);
			}
			
		}catch(Exception e){
			this.json = "[]";
		} 
		
		long endTime = System.currentTimeMillis();
		System.out.println("refreshJson Time Used："+(endTime-startTime));
		
		return AJAX;
	}

	@Action(value="refreshJson4tree")
	public String refreshJson4tree() throws Exception {
		try{
			Map map = reportPeriodManager.getPagedReportPeriodByParentId(this.node, this.active_node_id, this.start, this.limit);				
			this.json = JSONUtil.getJsonString("include","count,list,reportPeriodTypeGuid,guid,code,name,isleaf,timetype,begin_time,end_time,duration,order_in_year,parent_termGuid,previous_termGuid,lastyear_termGuid",map);
		}catch(Exception e){
			this.json = "[]";
		} 

		return AJAX;
	}

	@Action(value="removeReportPeriod")
	public String removeReportPeriod() throws Exception {
		LazyDynaBean ldb = new  LazyDynaBean();
		try{
			int count = reportWindowPeriodManager.getCount("select count(*) from ReportWindowPeriod rwp where rwp.reportPeriod.guid='" + node + "'");
			if(count>0){
				ldb.set("success", false);
				ldb.set("reason", "本填报期在填报名录或者窗口期中已经使用，不能删除!");
				this.json = JSONObject.fromObject(ldb).toString();
			}else{
				List<ReportPeriod> list = reportPeriodManager.getReportPeriodByLastyearTermGuid(node);
				for(ReportPeriod rp : list){
					rp.setLastyear_term(null);
					reportPeriodManager.saveObject(rp);
				}
				
				list = reportPeriodManager.getReportPeriodByParentTermGuid(node);
				for(ReportPeriod rp : list){
					rp.setParent_term(null);
					reportPeriodManager.saveObject(rp);
				}
				
				list = reportPeriodManager.getReportPeriodByPreviousTermGuid(node);
				for(ReportPeriod rp : list){
					rp.setPrevious_term(null);
					reportPeriodManager.saveObject(rp);
				}
				
				reportPeriodManager.removeObject(ReportPeriod.class, node);
				
				ldb.set("success", true);
				ldb.set("reason", "删除成功!");
				this.json = JSONObject.fromObject(ldb).toString();
			}
		}catch(Exception e){
			ldb.set("success", false);
			ldb.set("reason", e.getMessage());
			this.json = JSONObject.fromObject(ldb).toString();
			e.printStackTrace();
		}
		
		return AJAX;		
	}
	
	@Action(value="updateReportPeriod")
	public String updateReportPeriod() throws Exception {
		LazyDynaBean ldb = new  LazyDynaBean();
		try{
			ReportPeriodType rpt = (ReportPeriodType) reportPeriodTypeManager.getObject(ReportPeriodType.class, this.node);
			ReportPeriod[] ts = (ReportPeriod[])JSONUtil.stringToArray(json, ReportPeriod.class);
			for (ReportPeriod t : ts){
				if(t.getGuid()==null || t.getGuid().trim().length()==0 || t.getGuid().trim().startsWith("NEW")){
					t.setGuid(new GUID().toString());
				}
				if(t.getReportPeriodTypeGuid()==null || t.getReportPeriodTypeGuid().trim().length()==0){
					t.setReportPeriodType(rpt);
				}
				if(t.getParent_termGuid()==null || t.getParent_termGuid().trim().length()==0){
					t.setParent_term(null);
				}
				if(t.getPrevious_termGuid()==null || t.getPrevious_termGuid().trim().length()==0){
					t.setPrevious_term(null);
				}
				if(t.getLastyear_termGuid()==null || t.getLastyear_termGuid().trim().length()==0){
					t.setLastyear_term(null);
				}
				t.setService(this.getDefaultService());
				reportPeriodManager.saveObject(t);
			}
			
			ldb.set("success", true);
			ldb.set("reason", "保存成功!");
			this.json = JSONObject.fromObject(ldb).toString();
		}catch(Exception e){
			ldb.set("success", false);
			ldb.set("reason", e.getMessage());
			this.json = JSONObject.fromObject(ldb).toString();
			e.printStackTrace();
		}

		return AJAX;
	}
	
	@Action(value="addBatchReportPeriod")
	public String addBatchReportPeriod() throws Exception {
		LazyDynaBean ldb = new  LazyDynaBean();
		try{
			ReportPeriodType rpt = (ReportPeriodType) reportPeriodTypeManager.getObject(ReportPeriodType.class, this.node);
			String periodTypeCode = rpt.getCode().trim().toLowerCase();
			Calendar calendar = new GregorianCalendar();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String previousGuid = "";
			if(this.startdate==null || this.startdate.trim().length()==0){
				List<ReportPeriod> list = reportPeriodManager.find("from ReportPeriod t where t.reportPeriodType.guid='" + this.node + "' order by t.begin_time desc", 0, 1);
				if(list.size()==0){
					calendar.set(calendar.get(calendar.YEAR), 0, 1, 0, 0, 0);//初始化为当年的一月一日零时零分零秒
				}else{
					ReportPeriod rp = list.get(0);
					previousGuid = rp.getGuid();
					calendar.setTime(rp.getBegin_time());
				}
			}else{
				Date date1 = sdf.parse(this.startdate);
				calendar.setTime(date1);
				List<ReportPeriod> list = reportPeriodManager.find("from ReportPeriod t where t.reportPeriodType.guid='" + this.node + "' order by t.begin_time desc", 0, 1);
				if(list.size()>0){
					ReportPeriod rp = list.get(0);
					Date date2 = rp.getBegin_time();
					if(date2.after(date1)){
						calendar.setTime(date2);
					}
				}
			}
			
			if("day".equals(periodTypeCode)){	
				for(int i=0; i<this.interval; i++){
					ReportPeriod t = new ReportPeriod();
					String newGuid = new GUID().toString();
					t.setGuid(newGuid);
					t.setService(this.getDefaultService());
					
					int year = calendar.get(Calendar.YEAR);
					int month = calendar.get(Calendar.MONTH) + 1;
					int date = calendar.get(Calendar.DAY_OF_MONTH);
					
					calendar.set(Calendar.HOUR_OF_DAY,0);		
					calendar.set(Calendar.MINUTE,0);
					calendar.set(Calendar.SECOND,0);
					t.setBegin_time(calendar.getTime());
					
					calendar.set(Calendar.HOUR_OF_DAY,23);		
					calendar.set(Calendar.MINUTE,59);
					calendar.set(Calendar.SECOND,59);
					t.setEnd_time(calendar.getTime());					
					
					String code = periodTypeCode + ":" + String.valueOf(year) + "-" + StringUtils.leftPad(String.valueOf(month),2,"0") + "-" + StringUtils.leftPad(String.valueOf(date),2,"0");
					String lastyearCode = periodTypeCode + ":" + String.valueOf(year-1) + "-" + StringUtils.leftPad(String.valueOf(month),2,"0") + "-" + StringUtils.leftPad(String.valueOf(date),2,"0");
					String name = String.valueOf(year) + "年" + StringUtils.leftPad(String.valueOf(month),2,"0") + "月" + StringUtils.leftPad(String.valueOf(date),2,"0") + "日";
					t.setCode(code);
					t.setName(name);
					t.setDuration(1);
					t.setOrder_in_year(calendar.get(Calendar.DAY_OF_YEAR));
					
					calendar.set(Calendar.HOUR_OF_DAY,11);		
					calendar.set(Calendar.MINUTE,11);
					calendar.set(Calendar.SECOND,11);
					t.setTimetype(calendar.getTime());

					List list = reportPeriodManager.find("from ReportPeriod t where t.code='" + lastyearCode + "'");
					if(list.size()>0){
						ReportPeriod rp = (ReportPeriod)list.get(0);
						t.setLastyear_term(rp);
					}
					//t.setParent_term(parent_term)
					t.setPrevious_termGuid(previousGuid);
					t.setReportPeriodType(rpt);					
					
					previousGuid = newGuid;

					try{
						reportPeriodManager.saveObject(t);
					}catch(Exception e){
						//e.printStackTrace();
					}
					
					calendar.add(Calendar.DAY_OF_YEAR, 1);					
				}
				
			}else if("week".equals(periodTypeCode)){	
				for(int i=0; i<this.interval; i++){
					ReportPeriod t = new ReportPeriod();
					String newGuid = new GUID().toString();
					t.setGuid(newGuid);
					t.setService(this.getDefaultService());
					
					int year = calendar.get(Calendar.YEAR);					
					int week = calendar.get(Calendar.WEEK_OF_YEAR);
					
					calendar.set(Calendar.HOUR_OF_DAY,0);		
					calendar.set(Calendar.MINUTE,0);
					calendar.set(Calendar.SECOND,0);
					calendar.set(Calendar.DAY_OF_WEEK,2); //星期一
					t.setBegin_time(calendar.getTime());
					
					calendar.set(Calendar.HOUR_OF_DAY,23);		
					calendar.set(Calendar.MINUTE,59);
					calendar.set(Calendar.SECOND,59);
					calendar.set(Calendar.DAY_OF_WEEK,7); //星期六
					t.setEnd_time(calendar.getTime());					
					
					String code = periodTypeCode + ":" + String.valueOf(year) + "-" + StringUtils.leftPad(String.valueOf(week),2,"0");
					String lastyearCode = periodTypeCode + ":" + String.valueOf(year-1) + "-" + StringUtils.leftPad(String.valueOf(week),2,"0");
					String name = String.valueOf(year) + "年" + StringUtils.leftPad(String.valueOf(week),2,"0") + "周";
					t.setCode(code);
					t.setName(name);
					t.setDuration(1);
					t.setOrder_in_year(week);
					
					calendar.set(Calendar.HOUR_OF_DAY,11);		
					calendar.set(Calendar.MINUTE,11);
					calendar.set(Calendar.SECOND,11);
					calendar.set(Calendar.DAY_OF_WEEK,4); //星期三
					t.setTimetype(calendar.getTime());

					List list = reportPeriodManager.find("from ReportPeriod t where t.code='" + lastyearCode + "'");
					if(list.size()>0){
						ReportPeriod rp = (ReportPeriod)list.get(0);
						t.setLastyear_term(rp);
					}
					//t.setParent_term(parent_term)
					t.setPrevious_termGuid(previousGuid);
					t.setReportPeriodType(rpt);					
					
					previousGuid = newGuid;
					
					try{
						reportPeriodManager.saveObject(t);
					}catch(Exception e){
						//e.printStackTrace();
					}
					
					calendar.add(Calendar.WEEK_OF_YEAR, 1);					
				}
				
			}else if("month".equals(periodTypeCode)){	
				for(int i=0; i<this.interval; i++){
					ReportPeriod t = new ReportPeriod();
					String newGuid = new GUID().toString();
					t.setGuid(newGuid);
					t.setService(this.getDefaultService());
					
					int year = calendar.get(Calendar.YEAR);
					int month = calendar.get(Calendar.MONTH) + 1;
					
					calendar.set(Calendar.HOUR_OF_DAY,0);		
					calendar.set(Calendar.MINUTE,0);
					calendar.set(Calendar.SECOND,0);
					calendar.set(Calendar.DAY_OF_MONTH,1); 
					t.setBegin_time(calendar.getTime());
					
					calendar.set(Calendar.HOUR_OF_DAY,23);		
					calendar.set(Calendar.MINUTE,59);
					calendar.set(Calendar.SECOND,59);					
					if(month==2){
						calendar.set(Calendar.DAY_OF_MONTH,28); 
					}else{
						calendar.set(Calendar.DAY_OF_MONTH,30); 
					}
					t.setEnd_time(calendar.getTime());					
					
					String code = periodTypeCode + ":" + String.valueOf(year) + "-" + StringUtils.leftPad(String.valueOf(month),2,"0");
					String lastyearCode = periodTypeCode + ":" + String.valueOf(year-1) + "-" + StringUtils.leftPad(String.valueOf(month),2,"0");
					String name = String.valueOf(year) + "年" + StringUtils.leftPad(String.valueOf(month),2,"0") + "月";
					t.setCode(code);
					t.setName(name);
					t.setDuration(1);
					t.setOrder_in_year(month);
					
					calendar.set(Calendar.HOUR_OF_DAY,11);		
					calendar.set(Calendar.MINUTE,11);
					calendar.set(Calendar.SECOND,11);
					calendar.set(Calendar.DAY_OF_MONTH,15); 
					t.setTimetype(calendar.getTime());

					List list = reportPeriodManager.find("from ReportPeriod t where t.code='" + lastyearCode + "'");
					if(list.size()>0){
						ReportPeriod rp = (ReportPeriod)list.get(0);
						t.setLastyear_term(rp);
					}
					//t.setParent_term(parent_term)
					t.setPrevious_termGuid(previousGuid);
					t.setReportPeriodType(rpt);					
					
					previousGuid = newGuid;
					
					/*ReportPeriod old = (ReportPeriod)reportPeriodManager.getObject(ReportPeriod.class, code);
					if(old != null){
						reportPeriodManager.removeObject(ReportPeriod.class, code);
					}*/
					try{
						reportPeriodManager.saveObject(t);
					}catch(Exception e){
						//e.printStackTrace();
					}					
					
					calendar.add(Calendar.MONTH, 1);					
				}
				
			}else if("quarter".equals(periodTypeCode)){	
				for(int i=0; i<this.interval; i++){
					ReportPeriod t = new ReportPeriod();
					String newGuid = new GUID().toString();
					t.setGuid(newGuid);
					t.setService(this.getDefaultService());
					
					int year = calendar.get(Calendar.YEAR);
					int month_Begin = 0;
					int month_End = 1;
					int quarter = 1;					
					switch(calendar.get(Calendar.MONTH)){
					case 0:
					case 1:	
					case 2:	
						quarter = 1;
						month_Begin = 0;
						month_End = 2;
						break;
					case 3:	
					case 4:	
					case 5:	
						quarter = 2;
						month_Begin = 3;
						month_End = 5;
						break;
					case 6:	
					case 7:	
					case 8:	
						quarter = 3;
						month_Begin = 6;
						month_End = 8;
						break;
					case 9:	
					case 10:	
					case 11:	
						quarter = 4;
						month_Begin = 9;
						month_End = 11;
						break;
					}					
					
					calendar.set(Calendar.HOUR_OF_DAY,0);		
					calendar.set(Calendar.MINUTE,0);
					calendar.set(Calendar.SECOND,0);
					calendar.set(Calendar.MONTH,month_Begin); 
					calendar.set(Calendar.DAY_OF_MONTH,1); 
					t.setBegin_time(calendar.getTime());
					
					calendar.set(Calendar.HOUR_OF_DAY,11);		
					calendar.set(Calendar.MINUTE,11);
					calendar.set(Calendar.SECOND,11);
					calendar.set(Calendar.MONTH,month_Begin + 1); 
					calendar.set(Calendar.DAY_OF_MONTH,15); 
					t.setTimetype(calendar.getTime());
					
					calendar.set(Calendar.HOUR_OF_DAY,23);		
					calendar.set(Calendar.MINUTE,59);
					calendar.set(Calendar.SECOND,59);	
					calendar.set(Calendar.MONTH,month_End); 
					if(month_End==1){
						calendar.set(Calendar.DAY_OF_MONTH,28); //二月只有28天
					}else{
						calendar.set(Calendar.DAY_OF_MONTH,30); 
					}
					t.setEnd_time(calendar.getTime());					
					
					String code = periodTypeCode + ":" + String.valueOf(year) + "-" + String.valueOf(quarter);
					String lastyearCode = periodTypeCode + ":" + String.valueOf(year-1) + "-" + String.valueOf(quarter);
					String name = String.valueOf(year) + "年" + String.valueOf(quarter) + "季度";
					t.setCode(code);
					t.setName(name);
					t.setDuration(1);
					t.setOrder_in_year(quarter);					

					List list = reportPeriodManager.find("from ReportPeriod t where t.code='" + lastyearCode + "'");
					if(list.size()>0){
						ReportPeriod rp = (ReportPeriod)list.get(0);
						t.setLastyear_term(rp);
					}
					//t.setParent_term(parent_term)
					t.setPrevious_termGuid(previousGuid);
					t.setReportPeriodType(rpt);					
					
					previousGuid = newGuid;
					
					try{
						reportPeriodManager.saveObject(t);
					}catch(Exception e){
						//e.printStackTrace();
					}
					
					calendar.add(Calendar.MONTH, 1);					
				}
				
			}else if("halfyear".equals(periodTypeCode)){	
				for(int i=0; i<this.interval; i++){
					ReportPeriod t = new ReportPeriod();
					String newGuid = new GUID().toString();
					t.setGuid(newGuid);
					t.setService(this.getDefaultService());
					
					int year = calendar.get(Calendar.YEAR);
					int month_Begin = 0;
					int month_End = 1;
					int halfyear = 1;					
					switch(calendar.get(Calendar.MONTH)){
					case 0:
					case 1:	
					case 2:	
					case 3:	
					case 4:	
					case 5:	
						halfyear = 1;
						month_Begin = 0;
						month_End = 5;
						break;
					case 6:	
					case 7:	
					case 8:	
					case 9:	
					case 10:	
					case 11:	
						halfyear = 2;
						month_Begin = 6;
						month_End = 11;
						break;
					}					
					
					calendar.set(Calendar.HOUR_OF_DAY,0);		
					calendar.set(Calendar.MINUTE,0);
					calendar.set(Calendar.SECOND,0);
					calendar.set(Calendar.MONTH,month_Begin); 
					calendar.set(Calendar.DAY_OF_MONTH,1); 
					t.setBegin_time(calendar.getTime());
					
					calendar.set(Calendar.HOUR_OF_DAY,11);		
					calendar.set(Calendar.MINUTE,11);
					calendar.set(Calendar.SECOND,11);
					calendar.set(Calendar.MONTH,month_Begin + 1); 
					calendar.set(Calendar.DAY_OF_MONTH,15); 
					t.setTimetype(calendar.getTime());
					
					calendar.set(Calendar.HOUR_OF_DAY,23);		
					calendar.set(Calendar.MINUTE,59);
					calendar.set(Calendar.SECOND,59);	
					calendar.set(Calendar.MONTH,month_End); 
					if(month_End==1){
						calendar.set(Calendar.DAY_OF_MONTH,28); //二月只有28天
					}else{
						calendar.set(Calendar.DAY_OF_MONTH,30); 
					}
					t.setEnd_time(calendar.getTime());					
					
					String code = periodTypeCode + ":" + String.valueOf(year) + "-";
					String lastyearCode = periodTypeCode + ":" + String.valueOf(year-1) + "-";					
					String name = String.valueOf(year) + "年";
					if(halfyear==1){
						name += "上半年";
						code += "first half";
						lastyearCode += "first half";
					}else{
						name += "下半年";
						code += "second half"; 
						lastyearCode += "second half";
					}
					
					t.setCode(code);
					t.setName(name);
					t.setDuration(1);
					t.setOrder_in_year(halfyear);					

					List list = reportPeriodManager.find("from ReportPeriod t where t.code='" + lastyearCode + "'");
					if(list.size()>0){
						ReportPeriod rp = (ReportPeriod)list.get(0);
						t.setLastyear_term(rp);
					}
					//t.setParent_term(parent_term)
					t.setPrevious_termGuid(previousGuid);
					t.setReportPeriodType(rpt);					
					
					previousGuid = newGuid;
					
					try{
						reportPeriodManager.saveObject(t);
					}catch(Exception e){
						//e.printStackTrace();
					}
					
					calendar.add(Calendar.MONTH, 1);					
				}
				
			}else if("year".equals(periodTypeCode)){	
				for(int i=0; i<this.interval; i++){
					ReportPeriod t = new ReportPeriod();
					String newGuid = new GUID().toString();
					t.setGuid(newGuid);
					t.setService(this.getDefaultService());
					
					int year = calendar.get(Calendar.YEAR);
					int month_Begin = 0;
					int month_End = 11;										
					
					calendar.set(Calendar.HOUR_OF_DAY,0);		
					calendar.set(Calendar.MINUTE,0);
					calendar.set(Calendar.SECOND,0);
					calendar.set(Calendar.MONTH,month_Begin); 
					calendar.set(Calendar.DAY_OF_MONTH,1); 
					t.setBegin_time(calendar.getTime());
					
					calendar.set(Calendar.HOUR_OF_DAY,11);		
					calendar.set(Calendar.MINUTE,11);
					calendar.set(Calendar.SECOND,11);
					calendar.set(Calendar.MONTH,month_Begin + 6); 
					calendar.set(Calendar.DAY_OF_MONTH,15); 
					t.setTimetype(calendar.getTime());
					
					calendar.set(Calendar.HOUR_OF_DAY,23);		
					calendar.set(Calendar.MINUTE,59);
					calendar.set(Calendar.SECOND,59);	
					calendar.set(Calendar.MONTH,month_End); 
					if(month_End==1){
						calendar.set(Calendar.DAY_OF_MONTH,28); //二月只有28天
					}else{
						calendar.set(Calendar.DAY_OF_MONTH,30); 
					}
					t.setEnd_time(calendar.getTime());					
					
					String code = periodTypeCode + ":" + String.valueOf(year);
					String lastyearCode = periodTypeCode + ":" + String.valueOf(year-1);					
					String name = String.valueOf(year) + "年";
										
					t.setCode(code);
					t.setName(name);
					t.setDuration(1);
					t.setOrder_in_year(1);					

					List list = reportPeriodManager.find("from ReportPeriod t where t.code='" + lastyearCode + "'");
					if(list.size()>0){
						ReportPeriod rp = (ReportPeriod)list.get(0);
						t.setLastyear_term(rp);
					}
					//t.setParent_term(parent_term)
					t.setPrevious_termGuid(previousGuid);
					t.setReportPeriodType(rpt);					
					
					previousGuid = newGuid;
					
					try{
						reportPeriodManager.saveObject(t);
					}catch(Exception e){
						//e.printStackTrace();
					}
					
					calendar.add(Calendar.YEAR, 1);					
				}
				
			}

			ldb.set("success", true);
			ldb.set("reason", "保存成功!");
			this.json = JSONObject.fromObject(ldb).toString();
		}catch(Exception e){
			ldb.set("success", false);
			ldb.set("reason", e.getMessage());
			this.json = JSONObject.fromObject(ldb).toString();
			e.printStackTrace();
		}

		return AJAX;
	}
	
	public ReportPeriodTypeManager getReportPeriodTypeManager() {
		return reportPeriodTypeManager;
	}
	public void setReportPeriodTypeManager(ReportPeriodTypeManager reportPeriodTypeManager) {
		this.reportPeriodTypeManager = reportPeriodTypeManager;
	}
		
	public ReportPeriod getReportPeriod() {
		return reportPeriod;
	}

	public void setReportPeriod(ReportPeriod reportPeriod) {
		this.reportPeriod = reportPeriod;
	}

	public ReportPeriodManager getReportPeriodManager() {
		return reportPeriodManager;
	}

	public void setReportPeriodManager(ReportPeriodManager reportPeriodManager) {
		this.reportPeriodManager = reportPeriodManager;
	}

	public String getModalDialogFieldValue() {
		return ModalDialogFieldValue;
	}

	public void setModalDialogFieldValue(String modalDialogFieldValue) {
		ModalDialogFieldValue = modalDialogFieldValue;
	}

	public String getStartdate() {
		return startdate;
	}

	public void setStartdate(String startdate) {
		this.startdate = startdate;
	}

	public int getInterval() {
		return interval;
	}

	public void setInterval(int interval) {
		this.interval = interval;
	}

	public ReportWindowPeriodManager getReportWindowPeriodManager() {
		return reportWindowPeriodManager;
	}

	public void setReportWindowPeriodManager(
			ReportWindowPeriodManager reportWindowPeriodManager) {
		this.reportWindowPeriodManager = reportWindowPeriodManager;
	}
	
}
